package com.china08.yxyapi.api.manage.sch.platform.website.setting;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.entity.db.sws.SwsSetting.LogoStyle;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingRepository;

@CrossOrigin
@RestController("SwsSettingUpdDisplayStyleController")
public class SwsSettingUpdDisplayStyleController extends YxyApiBaseController {
    @Autowired
    private SwsSettingRepository swsSettingRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/setting/logo/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {

        String schoolId = getProfile(request, response).getSchoolId();

        String logoSetting = reqModel.logoSetting;
        String schoolLogo = reqModel.schoolLogo;
        String schoolName = reqModel.schoolName;
        String schoolBadge = reqModel.schoolBadge;
        String fontFamily = reqModel.fontFamily;
        Integer fontSize = reqModel.fontSize;
        String fontColor = reqModel.fontColor;

        Assert.isTrue(StringUtils.isNotBlank(logoSetting), "网站显示类型不能为空");
        Assert.isTrue(StringUtils.isNotBlank(schoolId), "学校ID不能为空");

        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);
        Assert.isTrue(null != swsSetting, "域名对象为空");
        swsSetting.setLogoSetting(LogoStyle.valueOf(logoSetting));

        if (logoSetting.equals("WEB_LOGO")) {
            Assert.isTrue(StringUtils.isNotBlank(schoolLogo), "网站logo不能为空");
            swsSetting.setSchoolLogo(schoolLogo);
        } else if (logoSetting.equals("BADGE_NAME")) {
            Assert.isTrue(StringUtils.isNotBlank(schoolBadge), "学校校徽不能为空");
            swsSetting.setSchoolBadge(schoolBadge);
            // Assert.isTrue(StringUtils.isNotBlank(fontFamily), "字体类型不能为空");
            // Assert.isTrue(fontSize > 11 && fontSize < 31, "字号不能为空");
            // Assert.isTrue(StringUtils.isNotBlank(fontColor), "字体颜色不能为空");
            if (StringUtils.isNotBlank(fontFamily)) {
                swsSetting.setFontFamily(fontFamily);
            } else {
                swsSetting.setFontFamily(null);
            }
            if (fontSize > 11 && fontSize < 31) {
                swsSetting.setFontSize(fontSize);
            } else {
                swsSetting.setFontSize(null);
            }
            if (StringUtils.isNotBlank(fontColor)) {
                swsSetting.setFontColor(fontColor);
            } else {
                swsSetting.setFontColor(null);
            }
        } else {
            Assert.isTrue(StringUtils.isNotBlank(schoolName), "学校名称不能为空");
            swsSetting.setSchoolName(schoolName);
            // Assert.isTrue(StringUtils.isNotBlank(fontFamily), "字体类型不能为空");
            // Assert.isTrue(fontSize > 11 && fontSize < 31, "字号不能为空");
            // Assert.isTrue(StringUtils.isNotBlank(fontColor), "字体颜色不能为空");
            if (StringUtils.isNotBlank(fontFamily)) {
                swsSetting.setFontFamily(fontFamily);
            } else {
                swsSetting.setFontFamily(null);
            }
            if (fontSize > 11 && fontSize < 31) {
                swsSetting.setFontSize(fontSize);
            } else {
                swsSetting.setFontSize(null);
            }
            if (StringUtils.isNotBlank(fontColor)) {
                swsSetting.setFontColor(fontColor);
            } else {
                swsSetting.setFontColor(null);
            }
        }
        swsSettingRepository.save(swsSetting);
        return true;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String logoSetting;
        public String schoolLogo;
        public String schoolName;
        public String schoolBadge;
        public String fontFamily;
        public Integer fontSize;
        public String fontColor;
    }

    protected class RespModel extends YxyApiRespModel {
    }
}
